package com.foreknow.demo4;

import com.sun.deploy.ui.AboutDialog;
import org.jcp.xml.dsig.internal.dom.DOMUtils;
import sun.management.snmp.jvmmib.EnumJvmThreadCpuTimeMonitoring;
import sun.plugin2.message.JavaScriptCallMessage;

import java.util.Scanner;

public class Exercise1 {

    //登录
    public static void welcome() {
        System.out.println("------------------------------------");
        System.out.println("--------------欢迎登陆---------------");
        System.out.println("--------------中国银行---------------");
        System.out.println("--------------版本1.0----------------");
        System.out.println("------------------------------------");
    }

        public static boolean login() {
        Scanner input = new Scanner(System.in);
        boolean isLogin = false;
        int i = 0;
        String rightname = "xw1025";
        String rightpassword = "wjy4321";
        for (i = 1;i <= 3;i++) {
            System.out.println("请输入账号：");
            String username = input.next();
            System.out.println("请输入密码：");
            String password = input.next();
                if (username.equals(rightname) && password.equals(rightpassword)) {
                    System.out.println("登陆成功！");
                    break;
                }else  if (i < 3) {
                        System.out.println("用户名或密码错误！请重新输入");
                        System.out.println("今日还有" + (3-i) + "次机会！");
                        isLogin = false;
                    }else {
                    System.out.println("错误输入3次  已吞卡  请找老司机处理");
                    isLogin = false;
                    System.exit(0);
                }
                }
        return isLogin;
        }

        //判断是否继续
    public static boolean decide() {
        boolean keep = false;
        Scanner input = new Scanner(System.in);
        System.out.println("是否继续操作(y/n)：");
        String answer = input.next();
        if ("y".equals(answer)) {
            keep = true;
        } else if ("n".equals(answer)) {
            keep = false;
        } else {
            System.out.println("输入错误！请重新输入");
            keep = true;
        }
        return keep;
    }

        //查询余额
    public static double select() {
        double yuE = 10000;
        return yuE;
    }

    //存款
    public static double putIn() {
        boolean great;
        Scanner input = new Scanner(System.in);
        do {
            System.out.println("请输入您要存款的金额：");
            double putmoney = input.nextDouble();
            if (putmoney > 10000) {
                System.out.println("ERROR！单次存款金额大于一万元");
                great = false;
                return 0;
            } else if (putmoney < 0) {
                System.out.println("ERROR！存款金额输入错误");
                great = false;
                return 0;
            } else if (putmoney % 100 != 0) {
                System.out.println("ERROR！请输入100的整数倍存款");
                great = false;
                return 0;
            } else {
                System.out.println("存款成功！");
                great = true;
                return putmoney;
            }
        } while (!great);
    }

    //取款
    //取款金额不大于账户余额？？
    public static double getOut() {
        boolean great;
        Scanner input = new Scanner(System.in);
        do {
            System.out.println("请输入您要取款的金额：");
            double getmoney = input.nextDouble();
//            if (getmoney > yuE) {
//                System.out.println("ERROR！您的余额不足");
//                great = false;
//            } else
            if (getmoney < 0) {
                System.out.println("ERROR！取款金额输入错误");
                great = false;
                return 0;
            } else if (getmoney % 100 != 0) {
                System.out.println("ERROR！请输入100的整数倍存款");
                great = false;
                return 0;
            } else if (getmoney > 3000) {
                System.out.println("ERROR！单次取款金额超过3000元");
                return 0;
            } else {
                System.out.println("取款成功！");
                great = true;
                return getmoney;
            }
        } while (!great);
    }

    //修改新密码
    public static String newupdate() {
        String newpassword = null;
        String oldpswd = "wjy4321";
        Scanner input = new Scanner(System.in);
        System.out.println("请输入新密码：");
        String newpwd1 = input.next();
        if (newpwd1.equals(oldpswd)) {
            System.out.println("ERROR！新密码不能与旧密码相同");
            return oldpswd;
        }
        System.out.println("请再次输入新密码：");
        String newpwd2 = input.next();
        if (newpwd1.equals(newpwd2)) {
            System.out.println("密码修改成功！");
            newpassword = newpwd1;
            return newpassword;
        } else if (!newpwd1.equals(newpwd2)) {
            System.out.println("两次输入密码不一致！请重新输入");
            return oldpswd;
        }
        return newpassword;
    }
    //验证旧密码
    public static String oldupdate() {
        boolean keep;
        String oldpswd = "wjy4321";
        Scanner input = new Scanner(System.in);
        System.out.println("请输入旧密码：");
        String oldpwd = input.next();
        if (oldpswd.equals(oldpwd)) {
            String newpassword = newupdate();
            return newpassword;
        }else {
                System.out.println("旧密码输入错误！请重新输入");
                return oldpswd;
            }
    }

    public static void main(String[] args) {
        boolean keep = false;
        Scanner input = new Scanner(System.in);
        welcome();
        login();
        double yuE = select();
        String answer = null;
        do {
            System.out.println("请选择操作序号：");
            int xuanze = input.nextInt();
        switch (xuanze) {
            case 1:
                System.out.println("您的余额为" + yuE);
                keep = decide();
                break;
            case 2:
                yuE += putIn();
                System.out.println("您当前余额为：" + yuE);
                keep = decide();
                break;
            case 3:
                yuE -= getOut();
                System.out.println("您当前的余额为：" + yuE);
                keep = decide();
                break;
            case 4:
                String mima = oldupdate();
                System.out.println("当前密码为：" + mima );
                keep = decide();
                break;
            case 5:
                System.out.println("感谢您的使用！");
                System.exit(0);
            keep = false;
                break;
            default:
                System.out.println("输入错误！请重新选择");
                keep = true;
        }
        }while (keep);
        System.out.println("感谢使用！");
    }
}










